using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using UWBGL_WinForms_Lib1;
using UWBGL_XNA_Lib13;

namespace XNA_SceneNode
{
	class DrawHandlerMinimap : UWB_XNAWindowHandler
	{
		//--------
		private int lastMouseX; // to + or - small view's size
		private bool _bZooming;
		int deltaX;

		private float previousRedboxX, previousRedboxY;
		private float mouseWorldX, mouseWorldY;

		private UWB_BoundingBox redBox; // bound for zoom-window
		private UWB_PrimitiveRectangle redRectPrimitive;

		//--------

		private UWBGL_UIWindow mWindow;

		public DrawHandlerMinimap()
		{
			mWindow = new UWBGL_UIWindow(true);
		}

		public bool Initialize(Control PlaceHolder)
		{
			redBox = new UWB_BoundingBox();
			redBox.setCorners(new Vector3(0, 0, 0), new Vector3(32, 20, 0));
			redRectPrimitive = new UWB_PrimitiveRectangle();

			mouseWorldX = mouseWorldY = 0;
			lastMouseX = 0;
			_bZooming = false;

			if (!mWindow.ReplaceFormControl(PlaceHolder.Handle))
				return false;

			if (!mWindow.AttachHandler(this))
				return false;

			return true;
		}

		// draws red-box too
		public override void DrawGraphics()
		{
			BeginScene();

			loadW2NDCXform();

			// drawing red-box
			Vector3 boxCenter = redBox.getCenter();
			float width = redBox.width();
			float height = redBox.height();
			Vector3 corner1 = new Vector3(boxCenter.X - (width / 2f), boxCenter.Y - (width / 2f), 0);
			Vector3 corner2 = new Vector3(boxCenter.X + (height / 2f), boxCenter.Y + (height / 2f), 0);

			redRectPrimitive.setCorners(corner1, corner2);
			redRectPrimitive.setFillMode(eFillMode.fmWireframe);
			redRectPrimitive.setFlatColor(Microsoft.Xna.Framework.Graphics.Color.Red);
			redRectPrimitive.setShadingColor(Microsoft.Xna.Framework.Graphics.Color.Red);
			redRectPrimitive.draw(eLevelofDetail.lodHigh, Program.getModel().getDrawHelper());
			// end drawing red-box

			Program.getModel().drawModel();

			EndSceneAndShow();
		}

		public override void OnMouseButton(bool down, MouseEventArgs e)
		{
			int deviceX = 0, deviceY = 0;
			hardwareToDevice(e.X, e.Y, ref deviceX, ref deviceY);

			switch (e.Button)
			{
				case MouseButtons.Left:
					if (down)
					{
						computeWorldCoordinates(deviceX, deviceY); // world coordinate
						redBox.moveToCenter(new Vector3(mouseWorldX, mouseWorldY, 0));
					}
					else
					{

					}
					break;
				case MouseButtons.Right:
					if (down)
					{
						_bZooming = true;
						lastMouseX = deviceX;
						previousRedboxX = redBox.width(); // save original dimensions
						previousRedboxY = redBox.height();
					}

					else
						_bZooming = false;
					break;
				case MouseButtons.Middle:
					break;
			}
		}

		public override void OnMouseMove(MouseEventArgs e)
		{
			int deviceX = 0, deviceY = 0;
			hardwareToDevice(e.X, e.Y, ref deviceX, ref deviceY);

			if (_bZooming)
			{
				deltaX = deviceX - lastMouseX;
				float zoomFactor = 1;// init

				if (deltaX > 50) // 2x limit
					zoomFactor = 2;
				if (deltaX < -45) // .1 limit
					zoomFactor = 0.1f;

				if (zoomFactor == 1) // limits not past
				{
					//int deltaX = deviceX - lastMouseX;
					if (deltaX > 0) // zoom in
						zoomFactor = (((float)deltaX) / 50f) + 1f;
					if (deltaX < 0) // zoom out
						zoomFactor = 1f - ((-(float)deltaX) / 45f) * 0.9f;
					if (deltaX == 0)
						zoomFactor = 1;
				}

				Vector3 boxCenter = redBox.getCenter();
				Vector3 corner1 = new Vector3(boxCenter.X - ((previousRedboxX * zoomFactor) / 2f), boxCenter.Y - ((previousRedboxX * zoomFactor) / 2f), 0);
				Vector3 corner2 = new Vector3(boxCenter.X + ((previousRedboxY * zoomFactor) / 2f), boxCenter.Y + ((previousRedboxY * zoomFactor) / 2f), 0);
				redBox.setCorners(corner1, corner2);
			}

			switch (e.Button)
			{
				case MouseButtons.Left:
					computeWorldCoordinates(deviceX, deviceY); // world coordinate
					redBox.moveToCenter(new Vector3(mouseWorldX, mouseWorldY, 0));
					break;
				case MouseButtons.Right:
					break;
				case MouseButtons.Middle:
					break;
			}
		}

		public void computeWorldCoordinates(int deviceX, int deviceY)
		{
			float worldHEIGHT = Program.getModel().getWorldBound().height();
			float worldWIDTH = Program.getModel().getWorldBound().width();

			float windowWidth = (float)mWindow.Width;
			float windowHeight = (float)mWindow.Height;

			mouseWorldX = (((float)deviceX) - (windowWidth / 2)) * (worldWIDTH / windowWidth) + worldWIDTH / 2;
			mouseWorldY = (((float)deviceY) - (windowHeight / 2)) * (worldHEIGHT / windowHeight) + worldHEIGHT / 2;
		}

		public UWB_BoundingBox getRedboxBounds()
		{
			return redBox;
		}

		public void setRedboxBounds(UWB_BoundingBox newRedbox)
		{
			redBox = newRedbox;
		}

	}
}
